﻿#include "emotionresource.h"
#include <QApplication>
#include <QDomDocument>
#include <QFile>
#include <QLocale>
#include <QTranslator>

const QSize Emotion::size(20, 20);

static const QString prefix = ":/emotion/";

static QPair<QMap<int, Emotion>, QHash<QString, int>> initEmotionResource()
{
    QPair<QMap<int, Emotion>, QHash<QString, int>> result;
    QFile file(prefix+"config.xml");
    if(!file.open(QFile::ReadOnly|QFile::Text))
        return result;
    QDomDocument doc;
    bool ok = doc.setContent(&file);
    file.close();
    if(ok)
    {
        QTranslator translator;
        translator.load("config_en", ":/emotion");
        QApplication::installTranslator(&translator);
        QDomElement root = doc.documentElement();
        QDomElement itemclass = root.firstChildElement("itemclass");
        int count = 0;
        auto &first = result.first;
        auto &second = result.second;
        while(!itemclass.isNull())
        {
            if(!itemclass.attribute("name").isEmpty())
            {
                count++;
                QDomElement itemElement = itemclass.firstChildElement();
                while(!itemElement.isNull())
                {
                    QDomNamedNodeMap attibutes = itemElement.attributes();
                    QDomNode node = attibutes.removeNamedItem("id");
                    auto id = node.nodeValue().toInt(&ok);
                    if(ok)
                    {
                        Emotion& item = first[id];
                        QString name = (node = attibutes.removeNamedItem("name")).nodeValue();
                        item.fileName =  (node = attibutes.removeNamedItem("image")).nodeValue();
                        item.pixmap = QPixmap::fromImage(QImage(prefix + item.fileName + QLatin1String(".png")).scaled(Emotion::size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));

                        second[name] = id;
                        QString translated = translator.translate("EmotionResource", name.toUtf8());
                        if(!translated.isEmpty() && translated != name)
                        {
                            if(translated != name)
                                second[translated] = id;
                            //让application选择语言
                            item.name = QApplication::translate("EmotionResource", name.toUtf8());
                        }
                        //没有英语翻译的不显示
                    }
                    itemElement = itemElement.nextSiblingElement();
                }
            }
            itemclass = itemclass.nextSiblingElement("itemclass");
        }
        QApplication::removeTranslator(&translator);
    }
    return result;
}

EmotionResource::EmotionResource() : emotions(initEmotionResource())
{
}

bool EmotionResource::containsName(const QString &name) const
{
    return emotions.second.contains(name);
}

QPixmap EmotionResource::findByFileName(const QString &fileName) const
{
    for(auto it = emotions.first.constBegin(); it != emotions.first.constEnd(); it++)
    {
        if(it.value().fileName == fileName)
            return it.value().pixmap;
    }
    return QPixmap();
}

QPixmap EmotionResource::findByName(const QString &name) const
{
    auto it = emotions.second.find(name);
    if(it == emotions.second.constEnd())
        return QPixmap();
    return emotions.first.value(it.value()).pixmap;
}
